package com.scsj.supermarket.view.activity.settingmodel;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.MineCommentAdapter;
import com.scsj.supermarket.base.AppCommodityEvaluationBase;
import com.scsj.supermarket.bean.AppCommodityEvaluationBean;
import com.scsj.supermarket.bean.SelectAppRankBean;
import com.scsj.supermarket.bean.parameter.AppCommodityEvaluationPostBean;
import com.scsj.supermarket.presenter.AppCommodityEvaluationPresenter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 我的评价（我曾经评价过的订单） 与MineCommentActivity 功能类似
 */
public class AllGoodsCommentActivity extends BaseActivity implements View.OnClickListener, AppCommodityEvaluationBase.IView {

    private RecyclerView commentRv;
    private MineCommentAdapter mineCommentAdapter;
    private TextView allComment, goodComment, midComment, badComment;
    private AppCommodityEvaluationPresenter presenter;
    private String goodsId;
    private String storeId;
    /**
     * 综合评价
     */
    private TextView mTopleftComprehensiveScoreTv;
    private XLHRatingBar mCommentRatingBarTop;
    /**
     * 商品评分
     */
    private TextView mMidTopScoreTv;
    private XLHRatingBar mCommentRatingBarBottom;
    /**
     * 4.0
     */
    private TextView mMidBottomScoreTv;
    /**
     * 5.0
     */
    private TextView mRightDeliveryScore;
    /**
     * 有图675
     */
    private TextView mCommentHavePicTv;
    private String flag;
    private static final String FLAGALL = "全部";
    private static final String FLAGGood = "好评";
    private static final String FLAGMID = "中评";
    private static final String FLAGBAD = "差评";
    private static final String FLAGHAVEPIC = "有图";


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_comm);
//        ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
//        //在setContentView();后面加上适配语句
//        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        presenter = new AppCommodityEvaluationPresenter(this);

        // TODO: 2018/7/5 店铺ID和商品ID暂时写死
//        goodsId = getIntent().getStringExtra("goodsId");
//        storeId = getIntent().getStringExtra("storeId");
        goodsId = "5dc87692-74fc-11e8-afb7-70106fb15682";
        storeId = "c1b9d0ec-749d-11e8-afb7-70106fb15682";

        AppCommodityEvaluationPostBean postBean = new AppCommodityEvaluationPostBean();
        postBean.setGoodsEvaluationDetail(new AppCommodityEvaluationPostBean.GoodsEvaluationDetailBean(goodsId, storeId));
        String s = new Gson().toJson(postBean);
        Logger.e("参数==》"+s);
        RequestBody body = RequestBody.create(MediaType
                .parse("application/json;charset=utf-8"), s);
        presenter.deliverData(body);

        getData(FLAGALL);
        initToolbar();
        setTitle("全部评价");
        hasBack(true);

        initView();
        initListener();
        initData();
    }

    private void getData(String flag) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("storeId", storeId);
        jsonObject.put("goodsId", goodsId);
        jsonObject.put("flag", flag);
        RequestBody body1 = RequestBody.create(MediaType
                .parse("application/json;charset=utf-8"), jsonObject.toString());
        presenter.deliverSelectData(body1);
    }

    @Override
    protected void initView() {
        commentRv = findViewById(R.id.comment_list_rv);
        allComment = findViewById(R.id.comment_all_tv);
        goodComment = findViewById(R.id.comment_good_tv);
        midComment = findViewById(R.id.comment_mid_tv);
        badComment = findViewById(R.id.comment_bad_tv);
        mTopleftComprehensiveScoreTv = (TextView) findViewById(R.id.topleft_comprehensive_score_tv);
        mCommentRatingBarTop = (XLHRatingBar) findViewById(R.id.comment_ratingBar_top);
        mMidTopScoreTv = (TextView) findViewById(R.id.mid_top_score_tv);
        mCommentRatingBarBottom = (XLHRatingBar) findViewById(R.id.comment_ratingBar_bottom);
        mMidBottomScoreTv = (TextView) findViewById(R.id.mid_bottom_score_tv);
        mRightDeliveryScore = (TextView) findViewById(R.id.right_delivery_score);
        mCommentHavePicTv = (TextView) findViewById(R.id.comment_have_pic_tv);

        commentRv.setLayoutManager(new LinearLayoutManager(this));
        commentRv.setNestedScrollingEnabled(false);
    }

    @Override
    protected void initListener() {

        allComment.setOnClickListener(this);
        goodComment.setOnClickListener(this);
        midComment.setOnClickListener(this);
        badComment.setOnClickListener(this);
        mCommentHavePicTv.setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.comment_all_tv:
                allComment.setBackgroundResource(R.drawable.red_border);
                goodComment.setBackgroundResource(R.drawable.mark_selected_shape);
                midComment.setBackgroundResource(R.drawable.mark_selected_shape);
                badComment.setBackgroundResource(R.drawable.mark_selected_shape);
                mCommentHavePicTv.setBackgroundResource(R.drawable.mark_selected_shape);
                flag = FLAGALL;
                break;
            case R.id.comment_good_tv:
                allComment.setBackgroundResource(R.drawable.mark_selected_shape);
                goodComment.setBackgroundResource(R.drawable.red_border);
                midComment.setBackgroundResource(R.drawable.mark_selected_shape);
                badComment.setBackgroundResource(R.drawable.mark_selected_shape);
                mCommentHavePicTv.setBackgroundResource(R.drawable.mark_selected_shape);
                flag = FLAGGood;
                break;
            case R.id.comment_mid_tv:
                allComment.setBackgroundResource(R.drawable.mark_selected_shape);
                goodComment.setBackgroundResource(R.drawable.mark_selected_shape);
                midComment.setBackgroundResource(R.drawable.red_border);
                badComment.setBackgroundResource(R.drawable.mark_selected_shape);
                mCommentHavePicTv.setBackgroundResource(R.drawable.mark_selected_shape);
                flag = FLAGMID;
                break;
            case R.id.comment_bad_tv:
                allComment.setBackgroundResource(R.drawable.mark_selected_shape);
                goodComment.setBackgroundResource(R.drawable.mark_selected_shape);
                midComment.setBackgroundResource(R.drawable.mark_selected_shape);
                badComment.setBackgroundResource(R.drawable.red_border);
                mCommentHavePicTv.setBackgroundResource(R.drawable.mark_selected_shape);
                flag = FLAGBAD;
                break;
            case R.id.comment_have_pic_tv:
                allComment.setBackgroundResource(R.drawable.mark_selected_shape);
                goodComment.setBackgroundResource(R.drawable.mark_selected_shape);
                midComment.setBackgroundResource(R.drawable.mark_selected_shape);
                badComment.setBackgroundResource(R.drawable.mark_selected_shape);
                mCommentHavePicTv.setBackgroundResource(R.drawable.red_border);
                flag = FLAGHAVEPIC;
                break;
            default:
                break;
        }

        getData(flag);
    }

    @Override
    public void showSuccessMsg(String s, AppCommodityEvaluationBean appCommodityEvaluationBean) {
        AppCommodityEvaluationBean.DataBean.AppCommodityBizDeliveryVoBean appCommodityBizDeliveryVo
                = appCommodityEvaluationBean.getData().getAppCommodityBizDeliveryVo();//获取4个评分
        mTopleftComprehensiveScoreTv.setText(appCommodityBizDeliveryVo.getSynthesizeGrade());
        mMidTopScoreTv.setText(appCommodityBizDeliveryVo.getGoodsGrade());
        mCommentRatingBarTop.setCountNum(appCommodityBizDeliveryVo.getGoodsGrade());
        mMidBottomScoreTv.setText(appCommodityBizDeliveryVo.getBizGrade());
        mCommentRatingBarBottom.setCountNum(appCommodityBizDeliveryVo.getBizGrade());
        mRightDeliveryScore.setText(appCommodityBizDeliveryVo.getDeliveryGrade());

        List<AppCommodityEvaluationBean.DataBean.AppCommodityStartVoBean> appCommodityStartVo
                = appCommodityEvaluationBean.getData().getAppCommodityStartVo();//全部，好评，中评，差评个数
        allComment.setText(appCommodityStartVo.get(0).getStartLevel() + " " + appCommodityStartVo.get(0).getStartLevelNum());
        goodComment.setText(appCommodityStartVo.get(1).getStartLevel() + " " + appCommodityStartVo.get(1).getStartLevelNum());
        midComment.setText(appCommodityStartVo.get(2).getStartLevel() + " " + appCommodityStartVo.get(2).getStartLevelNum());
        badComment.setText(appCommodityStartVo.get(3).getStartLevel() + " " + appCommodityStartVo.get(3).getStartLevelNum());
        mCommentHavePicTv.setText(appCommodityStartVo.get(4).getStartLevel() + " " + appCommodityStartVo.get(4).getStartLevelNum());


    }

    @Override
    public void showSelectSuccessMsg(String s, SelectAppRankBean selectAppRankBean) {

        /**
         * 网络图片集合 http://img1.3lian.com/2015/w10/51/d/27.jpg
         * 测试用
         */
//        List<String> listImg = new ArrayList<>();
//        listImg.add("http://img1.3lian.com/2015/w10/51/d/27.jpg");
//        listImg.add("http://img1.3lian.com/2015/w10/51/d/27.jpg");
//        listImg.add("http://img1.3lian.com/2015/w10/51/d/27.jpg");
//        listImg.add("http://img1.3lian.com/2015/w10/51/d/27.jpg");
//        listImg.add("http://img1.3lian.com/2015/w10/51/d/27.jpg");

        mineCommentAdapter = new MineCommentAdapter(selectAppRankBean.getData().getList(), this);
        commentRv.setAdapter(mineCommentAdapter);
    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this, s).show();
    }


}
